package com.fyj.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author 钰杰
 * @className RedisUtil
 * @description VX:FengYJ996
 * @date 2019/12/511:01
 */
@Component
public class RedisUtil {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 从redis获取dataSource
     *
     * @param mark
     * @param uuid
     * @return
     */
    public Map<String, String> generateDataSource(String mark, String uuid) {
        String driver = (String) stringRedisTemplate.opsForHash().get("datasource_" + mark + "_" + uuid, "driver");
        String url = (String) stringRedisTemplate.opsForHash().get("datasource_" + mark + "_" + uuid, "url");
        String username = (String) stringRedisTemplate.opsForHash().get("datasource_" + mark + "_" + uuid, "username");
        String password = (String) stringRedisTemplate.opsForHash().get("datasource_" + mark + "_" + uuid, "password");
        if (driver != null && driver.length() > 0) {
            HashMap<String, String> dataSource = new HashMap<>();
            dataSource.put("driver", driver);
            dataSource.put("url", url);
            dataSource.put("username", username);
            dataSource.put("password", password);
            addRedisMap(mark, uuid, dataSource);  //每从Redis获取数据源时，刷新存活时间
            return dataSource;
        }
        return null;
    }

    /**
     * redis存储dataSourceMap
     *
     * @param mark
     * @param uuid
     * @param dataSource 数据源
     */
    public void addRedisMap(String mark, String uuid, Map dataSource) {
        String dataSourceKey = "datasource_" + mark + "_" + uuid;
        stringRedisTemplate.opsForHash().putAll(dataSourceKey, dataSource);
        stringRedisTemplate.expire(dataSourceKey, 3, TimeUnit.MINUTES);
    }

    /**
     * 记录任务详情编号
     *
     * @param mark
     * @param uuid
     * @param dealKey 任务编号
     * @return
     */
    public boolean addMissionKey(String mark, String uuid, String dealKey) {
        String dataSourceKey = "datasource_" + mark + "_" + uuid;
        String checkAlive = (String) stringRedisTemplate.opsForHash().get(dataSourceKey, "username");
        if (checkAlive != null && checkAlive.length() > 0) {
            stringRedisTemplate.opsForValue().set("MissionDeatil" + mark + uuid, dealKey, 3, TimeUnit.MINUTES);
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取任务详情记录编号
     * @param uuid
     * @return
     */
    public HashMap<String, String> getMissionKey(String uuid) {
        HashMap<String, String> missionKeyMap = new HashMap<>();
        String sourceMissionKey = stringRedisTemplate.opsForValue().get("MissionDeatil" + 1 + uuid);//源数据库
        String targetMissionKey = stringRedisTemplate.opsForValue().get("MissionDeatil" + 2 + uuid);//目标数据库
        if (sourceMissionKey!=null && sourceMissionKey.length()>0 &&
        targetMissionKey!=null && targetMissionKey.length()>0){
            missionKeyMap.put("sourceKey",sourceMissionKey);
            missionKeyMap.put("targetKey",targetMissionKey);
            return missionKeyMap;
        }
        return null;
    }
}
